<?php


namespace app\api\controller;
use app\common\model\Member;
use app\validate\Check;
use library\Controller;
use think\Db;
use think\Request;
use app\validate\MasterCheck;
use app\common\model\Master as MasterModel;
use app\common\model\YinuoOrder;

class Master extends Base
{

    //管理后台
    public function index(Request $request,MasterCheck $check,MasterModel $masterModel){
        try {
            $param = $request->param();
            $result = $check->scene('master')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $masterModel->lists($param);
                if ($list) {
                    return $this->api_result(200, '请求成功', $list);
                } else {
                    return $this->api_result(400, '请求失败', $param);
                }
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }


    //管理员后台订单
    public function master_order(Request $request,MasterCheck $check,YinuoOrder $yinuoOrder){
        try {
            $param = $request->param();
            $result = $check->scene('master')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $yinuoOrder->order_list($param);
                if ($list) {
                    return $this->api_result(200, '请求成功', $list);
                } else {
                    return $this->api_result(400, '请求失败', $param);
                }
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }

    //我的电站信息
    public function my_station(Member $member, Request $request, Check $check){
        try {
            $param = $request->param();
            $result = $check->scene('user_info')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $member->my_station($param);
                return $this->api_result(200, '请求成功', $list);
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }

    //我的电桩信息
    public function my_device(Member $member, Request $request, Check $check){
        try {
            $param = $request->param();
            $result = $check->scene('user_info')->check($param);
            if (!$result) {
                return $this->api_result(400, $check->getError(), []);
            } else {
                $list = $member->my_device($param);
                return $this->api_result(200, '请求成功', $list);
            }
        } catch (\Exception $e) {
            return $this->api_result(400, '请求失败', []);
        }
    }


    //放行
    public function pass(){
        $order_id = input('order_id')??'';
        if(empty($order_id)){
            return $this->api_result(400, '订单未找到', []);
        }else{
            $order = Db::table("store_yinuo_order")->where("id=$order_id")->where("status=1")->find();
            if($order){
                Db::table("store_yinuo_order")->where("id=$order_id")->update(['status'=>2]);
                return $this->api_result(200, '请求成功', 1);
            }else{
                return $this->api_result(400, '订单未找到', []);
            }
        }
    }

}